﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;

using ihongma.WebData;


namespace ihongma.test
{
    public partial class cnblogs : System.Web.UI.Page
    {
        JumonyParser parser = new JumonyParser();
        protected void Page_Load(object sender, EventArgs e)
        {
            //http://www.cnblogs.com/lhb25/
           //Response.Write(url());
            //pageinfo("http://www.cnblogs.com/lhb25/archive/2011/05/31/2056103.html");
            //StringBuilder sb = new StringBuilder();
            //string showhtml = HttpRequestHelper.GetPageSourceByUrl("http://www.cnblogs.com/lhb25/default.html?page=1", "utf-8");
            //var dataDocument = parser.Parse(showhtml);
            //var items = dataDocument.Find(".postTitle a");
            //var ci = dataDocument.Find(".c_b_p_desc");
            //Response.Write(items.Count().ToString() + "yyy" + ci.Count().ToString());
           // Response.Write(url());
            for (int i=2; i<=2; i++)
            {
                url(i);
            }

        }
        public void url(int j)
        {
            //StringBuilder sb = new StringBuilder();
            string showhtml = HttpRequestHelper.GetPageSourceByUrl("http://www.cnblogs.com/lhb25/default.html?page="+j.ToString(), "utf-8");
            var dataDocument = parser.Parse(showhtml);
            var items = dataDocument.Find(".postTitle a");
            var ci = dataDocument.Find(".c_b_p_desc");
            var href = items.ToArray();
              var info=ci.ToArray();
              for (int i = 0; i < 2; i++)
            {
                //href.Count()
                    // sb.Append(href[i].Attribute("href").Value()+"<br/>");
                     //sb.Append(info[i].InnerText().Replace("摘要:","").Replace("阅读全文",""));

                pageinfo(href[i].Attribute("href").Value(), info[i].InnerText().Replace("摘要:","").Replace("阅读全文",""));
            }
            //foreach (var i in items)
            //{

            //    sb.Append(i.Attribute("href").Value()+"<br/>");
            //    sb.Append(i.InnerText());
            //}
           // return sb.ToString();

        }
        public void pageinfo(string usrl, string temp)
        {
            //StringBuilder sb = new StringBuilder();
            string showhtml = HttpRequestHelper.GetPageSourceByUrl(usrl, "utf-8");
            var dataDocument = parser.Parse(showhtml);
            var items = dataDocument.Find("#cnblogs_post_body");
            items.Find("ul").Remove();
            items.Find("p").Reverse().Take(3).Remove();
            var img = "";
            try
            {
                img = items.Find("img").ElementAt(0).Attribute("src").Value();
            }
            catch (Exception)
            {

                img = "/upload/null.jpg";
            }
            com.ihongma.www.addserver add = new com.ihongma.www.addserver();
            com.ihongma.www.article ar = new com.ihongma.www.article()
                            {
                                add_time = DateTime.Parse(dataDocument.Find("#post-date").Single().InnerText()),
                                author = "colt",
                                category_id = 350,
                                channel_id = 1,
                                zhaiyao = temp,
                                click = 100,
                                content = items.Single().ToString().Replace("您可能感兴趣的相关文章", "").Replace("您可能还喜欢", ""),
                                //dataDocument.Find("#artical_real p").ElementAt(0).InnerHtml() + "<p>" + temp + "</p>",

                                title = dataDocument.Find(".postTitle2").Single().InnerText(),
                                digg_act = 0,
                                digg_good = 0,
                                form = "鸿马",
                                img_url = img,
                                is_hot = 0,
                                is_lock = 0,
                                is_msg = 0,
                                is_red = 0,
                                is_slide = 0,
                                is_top = 0,
                                seo_description = temp,
                                seo_keywords = dataDocument.Find(".postTitle2").Single().InnerText(),
                                seo_title = dataDocument.Find(".postTitle2").Single().InnerText(),
                                sort_id = 99,
                                link_url = ""

                            };
            add.addnews(ar, "s123456");

        }
    }
}